//
//  FQHomeHeaderView.swift
//  SwiftProject
//
//  Created by 董駸 on 2023/4/14.
//

import UIKit

class FQHomeHeaderView: UIView, FSPagerViewDelegate, FSPagerViewDataSource {

    let pagerView = FSPagerView()
    let imageArray : [String] = ["home_bannar_1", "home_bannar_2"]
    
    var buttonBlock : (()->Void)?
    
    lazy var pageControl: FSPageControl = {
        let pageControl = FSPageControl()
        pageControl.contentInsets = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
        pageControl.setImage(UIImage.qmui_image(with: .white, size: CGSize(width: 4, height: 4), cornerRadius: 2), for: .normal)
        pageControl.setImage(UIImage.qmui_image(with: .white, size: CGSize(width: 10, height: 4), cornerRadius: 2), for: .selected)
        pageControl.contentHorizontalAlignment = .center
        pageControl.hidesForSinglePage = true
        return pageControl
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        clipsToBounds = true
        
        let whiteBackground = UIImageView(imageString: "home_background2_3")
        whiteBackground.layer.cornerRadius = 8
        whiteBackground.clipsToBounds = true
        whiteBackground.isUserInteractionEnabled = true
        addSubview(whiteBackground)
        
        whiteBackground.snp.makeConstraints { make in
            make.left.equalTo(20);
            make.right.equalTo(-20);
            make.top.equalTo(20);
            make.height.equalTo(147~);
        }
        
        let titleLb = QMUILabel(font: .rSFont(14), textColor: .white)
        titleLb.text = "最高可借额度(元)"
        whiteBackground.addSubview(titleLb)
        
        titleLb.snp.makeConstraints { make in
            make.left.equalTo(24)
            make.top.equalTo(24~)
        }
        
        let amountLb = QMUILabel(font: .mSFont(30), textColor: .white)
        amountLb.text = "200,000"
        whiteBackground.addSubview(amountLb)
        
        amountLb.snp.makeConstraints { make in
            make.left.equalTo(24)
            make.top.equalTo(titleLb.snp.bottom).offset(12~)
        }
        
        let rateLb = QMUILabel(font: .rSFont(12), textColor: .white)
        rateLb.text = "年化利率(单利) 7.2%～24%"
        whiteBackground.addSubview(rateLb)
        
        rateLb.snp.makeConstraints { make in
            make.left.equalTo(24)
            make.top.equalTo(amountLb.snp.bottom).offset(12~)
        }
        
        let activateBtn = QMUIFillButton(fill: .white, titleTextColor: .colorMain)
        activateBtn.titleLabel?.font = .bSFont(13)
        activateBtn.setTitle("查看额度", for: .normal)
        activateBtn.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
        whiteBackground.addSubview(activateBtn)
        
        activateBtn.snp.makeConstraints { make in
            make.width.equalTo(88)
            make.right.equalTo(-24)
            make.bottom.equalTo(-34)
            make.height.equalTo(35)
        }
        
        let messageV = UIView()
        messageV.backgroundColor = .white
        messageV.clipsToBounds = true
        messageV.layer.cornerRadius = 4
        addSubview(messageV)
        
        messageV.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.height.equalTo(34)
            make.top.equalTo(whiteBackground.snp.bottom).offset(16)
        }
        
        let arrowImage = ArrowButton(arrowSize: CGSize(width: 7, height: 10), arrowColor: .color102)
        messageV.addSubview(arrowImage)
        
        arrowImage.snp.makeConstraints { make in
            make.right.equalTo(-15)
            make.size.equalTo(CGSize(width: 7, height: 10))
            make.centerY.equalToSuperview()
        }
        
        let imageV = UIImageView(imageString: "home_ring")
        messageV.addSubview(imageV)
        
        imageV.snp.makeConstraints { make in
            make.left.equalTo(14)
            make.centerY.equalToSuperview()
        }
        
        let messageLb = QMUILabel(font: .rSFont(12), textColor: .color51)
        messageLb.text = "您有一条拒件记录"
        messageV.addSubview(messageLb)
        
        messageLb.snp.makeConstraints { make in
            make.left.equalTo(imageV.snp.right).offset(7)
            make.centerY.equalToSuperview()
            make.right.equalTo(arrowImage.snp.left).offset(-5)
        }
        
        pagerView.register(FSPagerViewCell.self, forCellWithReuseIdentifier: "cell")
        pagerView.itemSize = FSPagerView.automaticSize
        pagerView.dataSource = self
        pagerView.delegate = self
        pagerView.automaticSlidingInterval = 5
        pagerView.isInfinite = true
        pagerView.layer.cornerRadius = 10
        pagerView.clipsToBounds = true
        pagerView.removesInfiniteLoopForSingleItem = true
        addSubview(pagerView)

        pagerView.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.top.equalTo(messageV.snp.bottom).offset(16)
            make.height.equalTo(66~)
            make.right.equalTo(-20)
        }
        
        pagerView.addSubview(pageControl)
        pageControl.snp.makeConstraints { make in
            make.bottom.equalTo(pagerView)
            make.height.equalTo(20)
            make.left.right.equalTo(pagerView)
        }
        
        pageControl.numberOfPages = imageArray.count
        
        let colorView = UIView()
        colorView.backgroundColor = .colorMake(252, 236, 221)
        addSubview(colorView)
        
        colorView.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.top.equalTo(pagerView.snp.bottom).offset(16)
            make.height.equalTo(61)
        }
        
        let colorLb1 = QMUILabel(font: .mSFont(18), textColor: .colorMake(82, 43, 0))
        colorLb1.text = "为你推荐"
        colorView.addSubview(colorLb1)
        
        colorLb1.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.top.equalTo(17)
        }
        
        let colorLb2 = QMUILabel(font: .mSFont(11), textColor: .colorMake(82, 43, 0))
        colorLb2.text = "根据您的信用情况匹配不同的资方"
        colorView.addSubview(colorLb2)
        
        colorLb2.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.top.equalTo(colorLb1.snp.bottom).offset(7)
        }
        
        let diamondV = UIImageView(imageString: "home_diamond")
        diamondV.contentMode = .scaleAspectFill
        colorView.addSubview(diamondV)
        
        diamondV.snp.makeConstraints { make in
            make.right.top.equalToSuperview()
        }
        
        layoutIfNeeded()
        qmui_height = colorView.qmui_bottom
    }
    
    
    public func numberOfItems(in pagerView: FSPagerView) -> Int {
        imageArray.count
    }
    
    public func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", at: index)
        cell.imageView?.image = UIImage(named: imageArray[index])
        cell.contentView.layer.shadowColor = UIColor.clear.cgColor
        cell.contentView.backgroundColor = .clear
        return cell
    }

    func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
        pagerView.deselectItem(at: index, animated: true)
        if index == 1 {
            qmui_viewController?.navigationController?.qmui_pushViewController(FQExplainViewController(type: 1), animated: true)
        }
    }
    
    func pagerViewWillEndDragging(_ pagerView: FSPagerView, targetIndex: Int) {
        pageControl.currentPage = targetIndex
    }

    func pagerViewDidEndScrollAnimation(_ pagerView: FSPagerView) {
        pageControl.currentPage = pagerView.currentIndex
    }
    
    @objc func buttonClick() {
        buttonBlock?()
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
